//
//  NSBundle+OnlineRead.m
//  ZTEOnlineDoc
//
//  Created by HJiang on 2018/9/11.
//  Copyright © 2018年 ZTE. All rights reserved.
//

#import "NSBundle+OnlineRead.h"
#import "ZTEOnlineDoc.h"

@interface NSBundle()

/**
 * 语言
 */
@property (nonatomic,assign) ZODLanguage language;

@end

@implementation NSBundle (OnlineRead)

+ (instancetype)onLineRead_Bundle{
    static NSBundle *onLineReadBundle = nil;
    if (onLineReadBundle == nil) {
        // 这里不使用mainBundle是为了适配pod 1.x和0.x
        onLineReadBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[ZTEOnlineDoc class]] pathForResource:@"OnlineRead" ofType:@"bundle"]];
    }
    return onLineReadBundle;
}

+ (void)setupLanguage:(ZODLanguage)language{
    NSBundle *bundel = [NSBundle onLineRead_Bundle];
    bundel.language = language;
}

+ (UIImage *)onLineRead_arrowImage{
    static UIImage *arrowImage = nil;
    if (arrowImage == nil) {
        arrowImage = [[UIImage imageWithContentsOfFile:[[self onLineRead_Bundle] pathForResource:@"arrow@2x" ofType:@"png"]] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    }
    return arrowImage;
}

+ (NSString *)onLineRead_localizedStringForKey:(NSString *)key value:(NSString *_Nullable)value{
    static NSBundle *bundle = nil;
    if (bundle == nil) {
        // （iOS获取的语言字符串比较不稳定）目前框架只处理en、zh-Hans、zh-Hant三种情况，其他按照系统默认处理
//        NSString *language = [NSLocale preferredLanguages].firstObject;
//        if ([language hasPrefix:@"en"]) {
//            language = @"en";
//        } else if ([language hasPrefix:@"zh"]) {
//            if ([language rangeOfString:@"Hans"].location != NSNotFound) {
//                language = @"zh-Hans"; // 简体中文
//            } else { // zh-Hant\zh-HK\zh-TW
//                language = @"zh-Hant"; // 繁體中文
//            }
//        } else {
//            language = @"en";
//        }
        
        NSString *languageStr = @"en";
        ZODLanguage language = [NSBundle onLineRead_Bundle].language;
        
        if(language == ZODLanguageZh){
            languageStr = @"zh-Hans";
        }
        bundle = [NSBundle bundleWithPath:[[NSBundle onLineRead_Bundle] pathForResource:languageStr ofType:@"lproj"]];
    }
    value = [bundle localizedStringForKey:key value:value table:nil];
    return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
}

+ (NSString *)onLineRead_localizedStringForKey:(NSString *)key{
    return [self onLineRead_localizedStringForKey:key value:nil];
    
}

@end
